package com.dongdongshop.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.dongdongshop.mapper.TbGoodsDescMapper;
import com.dongdongshop.mapper.TbGoodsMapper;
import com.dongdongshop.mapper.TbItemMapper;
import com.dongdongshop.seller.pojo.TbGoods;
import com.dongdongshop.seller.pojo.TbGoodsDesc;
import com.dongdongshop.seller.pojo.TbItem;
import com.dongdongshop.seller.service.GoodsService;
import com.dongdongshop.seller.vo.GoodsVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Component
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private TbGoodsMapper goodsMapper;
    @Autowired
    private TbGoodsDescMapper goodsDescMapper;
    @Autowired
    private TbItemMapper itemMapper;

    @Override
    @Transactional
    public void addGoodsAndGoodsDesc(TbGoods goods, TbGoodsDesc goodsDesc, List<TbItem> items) {
        //增加商品表并返回id
        goodsMapper.insertGoods(goods);
        Long id = goods.getId();
        //增加商品详情表
        goodsDesc.setGoodsId(id);
        goodsDescMapper.insertSelective(goodsDesc);
        /*items.stream().forEach(item -> item.setGoodsId(id));*/
        for (TbItem item : items) {
            item.setGoodsId(id);
        }
        itemMapper.addTbItem(items);

    }

    @Override
    public List<TbGoods> getGoodsByName(String goodsName) {
        List<TbGoods> goods = goodsMapper.getGoodsByName(goodsName);
        return goods;
    }

    @Override
    public void deleteGoods(Long[] ids) {
        goodsMapper.deleteGoods(ids);
    }

    @Override
    public void updateStatus(Integer obj,Long[] ids) {
        goodsMapper.updateStatus(obj,ids);
    }

    @Override
    public List<TbGoods> getGoodsBySellerId(String sellerId,Integer state,String name) {

        List<TbGoods>goodsList = goodsMapper.getGoodsBySellerId(sellerId,state,name);
        return goodsList;
    }

    @Override
    public GoodsVO selectById(Long id) {
        GoodsVO goodsVO = goodsMapper.selectById(id);
        return goodsVO;
    }

    @Override
    public TbGoods getGoodsByGoodsId(Long goodsId) {
        TbGoods goods = goodsMapper.selectByPrimaryKey(goodsId);
        return goods;
    }
}
